import pymysql

class Database:
    aurl = '127.0.0.1'
    user = 'sa'
    password = '123456'
    db = 'test'
    charset = 'utf8'

    def __init__(self):
        self.connection = pymysql.connect(self.aurl, self.user, self.password, self.db, charset=self.charset)
        self.cursor = self.connection.cursor()

    def insert(self, query):
        try:
            self.cursor.execute(query)
            self.connection.commit()
        except Exception as e:
            print (e)
            self.connection.rollback()

    def query(self, query):
        cursor = self.connection.cursor(pymysql.cursors.DictCursor)
        cursor.execute(query)
        return cursor.fetchall()

    def __del__(self):
        self.connection.close()

    def test_query(articleurl):
        mysql = Database()
        try:
            querySql = "SELECT * FROM `article` WHERE url='" + articleurl + "'"
            chanels = mysql.query(querySql)
            return chanels
        except Exception as e:
            print(e)

if __name__ == "__main__":
    a = Database.test_query('tips')
